//
//  CorgiComposeCollectionView.swift
//  weibo
//
//  Created by Corgi on 17/3/28.
//  Copyright © 2017年 cocoaHangTou. All rights reserved.
//

import UIKit

class CorgiComposeCollectionView: UICollectionView {

    var closure: (() -> ())?
    
    var imageList: [UIImage] = [UIImage]()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowLayout = UICollectionViewFlowLayout()
        
        flowLayout.itemSize = CGSize(width: cellWH, height: cellWH)
        
        flowLayout.minimumLineSpacing = itemMargin
        
        flowLayout.minimumInteritemSpacing = itemMargin
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func addImage(img: UIImage) {
        
        imageList.insert(img, at: 0)
        
        isHidden = false
        
        if imageList.count > 9 {
            return
        }
        
        reloadData()
    }

    fileprivate func setupUI() {
        
        backgroundColor = UIColor.clear
        
        dataSource = self
        
        delegate = self
        
        isHidden = true
        
        register(CorgiComposeCollectionCell.self, forCellWithReuseIdentifier: "cell")
    }
}

extension CorgiComposeCollectionView: UICollectionViewDataSource, UICollectionViewDelegate {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    
        let count = imageList.count
        
        if count == 0 || count == 9 {
        
            return count
        }
        
        return count + 1
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CorgiComposeCollectionCell
        
        if indexPath.item == imageList.count {
            
            cell.image = nil
            
        } else {
        
            cell.image = imageList[indexPath.item]
            
            cell.closure = { [weak self] in
            
                self?.imageList.remove(at: indexPath.item)
            
                if self?.imageList.count == 0 {
                
                    self?.isHidden = true
                }
                
                self?.reloadData()
            }
        }
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        collectionView.deselectItem(at: indexPath, animated: true)
        
        if indexPath.item == imageList.count {
        
            closure?()
        }
    }
}

class CorgiComposeCollectionCell: UICollectionViewCell {
    
    var image: UIImage? {
    
        didSet {
        
            if image == nil {
            
                bigImageView.image = UIImage(named: "compose_pic_add")
            
                bigImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                
                closeButton.isHidden = true
                
            } else {
            
                bigImageView.image = image
                
                closeButton.isHidden = false
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var closure:(() -> ())?
    
    private func setupUI() {

        backgroundColor = UIColor.clear
        
        contentView.addSubview(bigImageView)
        
        contentView.addSubview(closeButton)
        
        closeButton.snp.makeConstraints { (make) in
            make.top.right.equalToSuperview()
        }
        
        bigImageView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    private lazy var bigImageView: UIImageView = UIImageView()
    
    private lazy var closeButton: UIButton = {
    
        let btn = UIButton()
    
        btn.addTarget(self, action: #selector(closeButtonClick), for: .touchUpInside)
        
        btn.setImage(UIImage(named:"compose_photo_close"), for: .normal)
    
        return btn
    }()
}

extension CorgiComposeCollectionCell {

    @objc fileprivate func closeButtonClick() {
        closure?()
    }
}
